function idat = fsb_ssmooth_idat(idat,param,kroon)

% FSB : Spatial smoothing of 4D images
%
% EXAMPLE:
% idat = fsb_ssmooth_idat(idat,3)
%
% INPUT:
% idat:            4D image data set
% param:           smoothing kernel size in voxels
% kroon:         set this to one to use the imgaussian smoothing
% function, omit it or set it to zero to use the standard matlab function
%
% OUTPUT:
% a spatially smoothed 4D dataset
%
% CALLED BY:
% FSB.m
% fsb_remove_disc.m
%
% NOTES:
% This function belongs to fMRI Sandbox and calls the function
% imgaussian.m from Dirk-Jan Kroon's imgaussian smoothing package.
% available at
% http://www.mathworks.com/matlabcentral/fileexchange/25397-imgaussian
% Defaults to the standard Matlab smoothing routine if imgaussian is not
% available
%
%% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
%
% Revision 1.0
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

tic;
%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Determine image size
%~~~~~~~~~~~~~~~~~~~~~~~~~~
[a,b,c,d] = size(idat);
idat = single(idat);
ssmooth_vox = round(param*2.355);

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Graphics output
%~~~~~~~~~~~~~~~~~~~~~~~~~~
waitstring = ['Smoothing by ' num2str(ssmooth_vox) ' voxels with Gaussian kernel...'];
disp(waitstring);
h = waitbar(0,waitstring);

%~~~~~~~~~~~~~~~~~~~~~~~~~~
% Smoothing
%~~~~~~~~~~~~~~~~~~~~~~~~~~

if nargin>2 && kroon ==1;
    try
        for x = 1:d;
            % idat(:,:,:,x) = smooth3(idat(:,:,:,x),'gaussian',3,param);
            idat(:,:,:,x) = imgaussian(idat(:,:,:,x),param);
            t = toc;
            waitstring = ['Smoothing by ' num2str(ssmooth_vox) ' voxels with Imgaussian function, ' num2str(int16(t)) ' s elapsed'];
            waitbar(x/d,h,waitstring)
        end
    catch
        disp('imgaussian function not yet compiled or installed, please install first')
        for x = 1:d;
            idat(:,:,:,x) = smooth3(idat(:,:,:,x),'gaussian',3,param);
            %idat(:,:,:,x) = smooth3f(idat(:,:,:,x),'gaussian',9,param);
            t = toc;
            waitstring = ['Smoothing by ' num2str(ssmooth_vox) ' voxels with Gaussian kernel, ' num2str(int16(t)) ' s elapsed'];
            waitbar(x/d,h,waitstring)
        end
    end

else
    for x = 1:d;
        idat(:,:,:,x) = smooth3(idat(:,:,:,x),'gaussian',3,param);
        %idat(:,:,:,x) = smooth3f(idat(:,:,:,x),'gaussian',9,param);
        t = toc;
        waitstring = ['Smoothing by ' num2str(ssmooth_vox) ' voxels with Gaussian kernel, ' num2str(int16(t)) ' s elapsed'];
        waitbar(x/d,h,waitstring)
    end
end

close(h) ;

end
